package com.apps.ftapp.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.SubjectAdapter;


import com.apps.ftapp.model.SubjectModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.BroadCastManager;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cz.msebera.android.httpclient.Header;

public class SubjectActivity extends AppCompatActivity implements View.OnClickListener, SubjectAdapter.MyClickListener {

    @BindView(R.id.cus_title)
    TextView cusTitle;
    @BindView(R.id.listView)
    ListView listView;
    Unbinder unbinder;
    private List<SubjectModel.CategoriesBean> dataList = new ArrayList<>();
    private SubjectAdapter subjectAdapter;

    private String yiji_name;

    private Gson gson = new Gson();

    private LocalReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject);
        ButterKnife.bind(this);
        //接收广播
        try {
            IntentFilter filter = new IntentFilter();
            filter.addAction("SubjectFragment");
            mReceiver = new LocalReceiver();
            BroadCastManager.getInstance().registerReceiver(this,
                    mReceiver, filter);//注册广播接收者
        } catch (Exception e) {
            e.printStackTrace();
        }
        init();
    }


    private void init() {
        initView();


        loadData();
    }


    private void loadData() {

        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("page", 1);
        Async.post("tiku/category", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    //SubjectModel
                    try {
                        SubjectModel result = gson.fromJson(new String(responseBody), SubjectModel.class);
                        if (1 == Tools.isIntNull(result.getSuccess())) {
                            if (null != result.getCategories() && result.getCategories().size() > 0) {
                                dataList.addAll(result.getCategories());
                                subjectAdapter.addRes(result.getCategories());

                            }
                        }
                    } catch (Exception e) {

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }


    private void initView() {


        yiji_name = SharedPreferencesUtil.getData(getApplicationContext(), "yijiName", "") + "";
        L.i("main", "yiji_name>>>>" + yiji_name);
        if (!"".equals(Tools.isNull(yiji_name))) {
            cusTitle.setText(Tools.isNull(yiji_name));
        } else {
            cusTitle.setText("自学考试");
        }

        cusTitle.setOnClickListener(this);

//        moreSubject.setOnClickListener(this);


        subjectAdapter = new SubjectAdapter(this, dataList, yiji_name, this);
        listView.setAdapter(subjectAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("id", dataList.get(i).getId());
                map.put("title", dataList.get(i).getName());
                ActivitySkipUtil.skipAnotherActivity(SubjectActivity.this, LianXiActivity.class, map);
            }
        });

//        refreshLayout.autoRefresh();
//        refreshLayout.finishLoadmore(0,false);

    }

    public void shuaxin() {
        yiji_name = SharedPreferencesUtil.getData(this, "yijiName", "") + "";
        L.i("main", "yiji_--------SubjectFragment------name>>>>" + yiji_name);
        if (!"".equals(Tools.isNull(yiji_name))) {
            cusTitle.setText(Tools.isNull(yiji_name));
        } else {
            cusTitle.setText("自学考试");
        }

        subjectAdapter = new SubjectAdapter(this, dataList, yiji_name, this);
        listView.setAdapter(subjectAdapter);
        subjectAdapter.notifyDataSetInvalidated();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more_subject:

                // ActivitySkipUtil.skipAnotherActivity(getActivity(), SubjectAddActivity.class,false);
                //  ActivitySkipUtil.skipAnotherActivity(getActivity(), LoginActivity.class,false);
                ActivitySkipUtil.skipAnotherActivity(this, SubjectMoreActivity.class, false);
                break;

            case R.id.cus_title:
                Constant.IS_EDIT_CHANAGE = "SubjectFragment";
                ActivitySkipUtil.skipAnotherActivity(this, SubjectMoreActivity.class, false);
                break;
        }
    }

    @Override
    public void clickListener(View v) {
        int i = (int) v.getTag();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("id", dataList.get(i).getId());
        map.put("title", dataList.get(i).getName());
        switch (v.getId()) {
            case R.id.zhangjieliebiao:
                ActivitySkipUtil.skipAnotherActivity(SubjectActivity.this, LianXiActivity.class, map);
                break;

            case R.id.kuaisulianxi:
                HashMap<String, Object> data = new HashMap<String, Object>();
                data.put("id", dataList.get(i).getId());
                data.put("keys", "single_choice");
//                data.put("keys","");
                data.put("keycur", 0);
                data.put("cccccc", "快速练习");
                data.put("lmname", "");
//                ActivitySkipUtil.skipAnotherActivity(LianXiActivity.this, GestureFilpActivity.class,data);

                ActivitySkipUtil.skipAnotherActivity(SubjectActivity.this, SubjectZdActivity.class, data);


                break;

            case R.id.subject_top:
                ActivitySkipUtil.skipAnotherActivity(SubjectActivity.this, LianXiActivity.class, map);
                break;
        }
    }

    class LocalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //收到广播后的处理

            shuaxin();

        }

    }
}
